﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration;
using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Web.Support;
using SIPO.Library.DomainObject.Administration.UserManagement;
using System.Globalization;
using System.Web.SessionState;

namespace SIPO.Web.Forms.Transaction
{
    public partial class recRestructurNextform : BasePage, IRequiresSessionState
    {
        private int id = -1;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
                hfID.Value = id.ToString();

                if (Request.Params["status"] != null) {
                    if (Request.Params["status"].ToString().Trim().Equals("view"))
                    {
                        List<recRestructur> listRes = null;
                        if (Request.Params["edit"] != null)
                        {
                            recRestructur ress = (recRestructur)Session["restructur"];
                            listRes = new List<recRestructur>();
                                listRes.Add(ress);
                           List<recRestructur> lista = (List<recRestructur>)Session["punyaseno"];
                           recRestructur resi = lista.Select(x => new recRestructur
                            {
                                RestructurID = x.RestructurID,
                                recRestructurSchedules = x.recRestructurSchedules
                            }).Where(x => x.RestructurID == id).ToList<recRestructur>()[0];
                           Session["resschema"] = resi.recRestructurSchedules;
                        }
                        else
                        {
                            if (Session["punyaseno"] != null) listRes = (List<recRestructur>)Session["punyaseno"];
                        }

                        if (listRes != null)
                        {
                            recRestructur res = listRes.Select(x => new recRestructur
                            {
                                RestructurID = x.RestructurID,
                                DebtorID = x.DebtorID,
                                DebtorName = x.DebtorName,
                                LetterNo = x.LetterNo,
                                LetterDate = x.LetterDate,
                                SumValues = x.SumValues,
                                recRestructurSchedules = x.recRestructurSchedules
                            }).Where(x => x.RestructurID == id).ToList<recRestructur>()[0];

                            txtNamaDebitur.Text = res.DebtorName;
                            txtNoSK.Text = res.LetterNo;
                            txtNilai.Text = String.Format("{0:0,00}", res.SumValues.ToString());
                            if (Request.Params["edit"] == null)
                            {
                                Session["resschema"] = res.recRestructurSchedules;
                            }
                            ClientScript.RegisterStartupScript(typeof(Page), "edit", "activateEdit();", true);
                        }
                        else {
                            ClientScript.RegisterStartupScript(typeof(Page),
                                    "ErrorInSession",
                                    "alert('Error data is empty');", true);
                        }        
                    }
                }
            }
            else {
                if (Request.Params["status"] != null) {
                    if (Request.Params["status"].ToString().Trim().Equals("addnew")) {
                        if (Session["restructur"] != null)
                        {
                            recRestructur res = (recRestructur)Session["restructur"];
                            txtNamaDebitur.Text = res.DebtorName;
                            txtNoSK.Text = res.LetterNo;
                            txtNilai.Text = String.Format("{0:0,00}", res.SumValues.ToString());
                        }
                        else {
                            ClientScript.RegisterStartupScript(typeof(Page),
                                "ErrorInSession",
                                "alert('Error data is empty');", true);
                        }
                    }
                }
            }

            if (!Page.IsPostBack)
            {
                GetPrivilege();
            }
        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').hide();", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        //ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }
    }
}